class SmartHud extends HUDKillingFloor;

// For mouse interface
var         bool            bCapturingMouse;
var         vector          MouseCurrentPos;
var         int             LastHUDSizeX, LastHUDSizeY;
var         bool            bHaveAtLeastOneValidMouseUpdate;
var(ROHud)  SpriteWidget    MouseInterfaceIcon;

var            AbsoluteCoordsInfo   MapLevelImageCoordinates;

simulated function DrawHud(Canvas C)
{

updateMouse();
//drawCursor();

	super.drawhud(c);

}


function updateMouse()
{
	// update mouse position for identification routines
	MouseInterfaceStartCapturing();
//	MouseCurrentPos = getMouseCoordinates();
	//nvas.DeProject(MouseCurrentPos,MouseLocation,MouseDirection);
}
/*
function Vector getMouseCoordinates()
{
	local Int ScreenPos;
	local vector MousePos,x ,y;

	//class'UIRoot'.static.GetCursorPosition(ScreenPos.X , ScreenPos.Y);

	MousePos.X = float(ScreenPos.X);
	MousePos.Y = float(ScreenPos.Y);

	return MousePos;
}
*/
function MouseInterfaceStartCapturing()
{
	bCapturingMouse = true;
	bHaveAtLeastOneValidMouseUpdate = false;
	AKFPlayer(PlayerOwner).bHudCapturesMouseInputs = true;
}

function MouseInterfaceStopCapturing()
{
	AKFPlayer(PlayerOwner).bHudCapturesMouseInputs = false;
	bCapturingMouse = false;
	MouseInterfaceUnlockPlayerRotation();
}

function MouseInterfaceUpdatePosition(vector newPos)
{
	local float PosX, PosY;

	MouseCurrentPos = newPos;
	bHaveAtLeastOneValidMouseUpdate = true;

//	if (bShowObjectives)
//	{
		PosX = MouseCurrentPos.X + LastHUDSizeX * 0.5;
		PosY = MouseCurrentPos.Y + LastHUDSizeY * 0.5;

		if (PosX > MapLevelImageCoordinates.PosX &&
			PosY > MapLevelImageCoordinates.PosY &&
			PosX < MapLevelImageCoordinates.width + MapLevelImageCoordinates.PosX &&
			PosY < MapLevelImageCoordinates.height + MapLevelImageCoordinates.PosY)
		{
			MouseInterfaceLockPlayerRotation();
		}
		else
			MouseInterfaceUnlockPlayerRotation();
//	}
//	else
//		MouseInterfaceUnlockPlayerRotation();
}

function MouseInterfaceClick()
{
//	if (bShowObjectives)
//	{
//		if (HandleLevelMapClick(MouseCurrentPos.X + LastHUDSizeX * 0.5, MouseCurrentPos.Y + LastHUDSizeY * 0.5))
//			HideObjectives();
	//}
	//else // Safeguard
	//	MouseInterfaceStopCapturing();
}

function MouseInterfaceDrawCursor(Canvas C)
{
	local float PosX, PosY;
	local vector newMousePos;

	if (!bHaveAtLeastOneValidMouseUpdate)
		return;

	newMousePos = MouseCurrentPos;

	PosX = newMousePos.X + C.SizeX * 0.5;
	PosY = newMousePos.Y + C.SizeY * 0.5;

	if (PosX < 0)
	{
		newMousePos.X -= PosX;
		PosX = 0;
	}
	else if (PosX >= C.ClipX)
	{
		newMousePos.X -= (PosX - C.SizeX);
		PosX = C.SizeX - 1;
	}

	if (PosY < 0)
	{
		newMousePos.Y -= PosY;
		PosY = 0;
	}
	else if (PosY >= C.ClipY)
	{
		newMousePos.Y -= (PosY - C.SizeY);
		PosY = C.SizeY - 1;
	}

	if (newMousePos != MousecurrentPos)
		ROPlayer(PlayerOwner).MouseInterfaceSetMousePos(newMousePos);

	//log("newMousePos.X = " $ newMousePos.X);
	//log("newMousePos.Y = " $ newMousePos.Y);

	MouseInterfaceIcon.PosX = PosX / C.ClipX;
	MouseInterfaceIcon.PosY = PosY / C.ClipY;

	LastHUDSizeX = C.ClipX;
	LastHUDSizeY = C.ClipY;

	//log("Drawing to " $ MouseInterfaceIcon.PosX $ "," $ MouseInterfaceIcon.PosY);

	DrawSpriteWidget(C, MouseInterfaceIcon);
}

function MouseInterfaceLockPlayerRotation()
{
	if (ROPlayer(PlayerOwner) != none)
		AKFPlayer(PlayerOwner).MouseInterfaceSetRotationLock(true);
}

function MouseInterfaceUnlockPlayerRotation()
{
	if (AKFPlayer(PlayerOwner) != none)
		AKFPlayer(PlayerOwner).MouseInterfaceSetRotationLock(false);
}
